﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Helper;
using CoreData;

namespace Services.BusinessRules
{
    public class ValidateExist:BusinessObject
    {

       string NameEN;
       string NameVN;
       string FieldName;
       public ValidateExist(String namevn, string nameen, string FieldName, object object1, IList<object> object2)
       {
           this.NameVN = namevn;
           this.NameEN = nameen;
           this.FieldName = FieldName;
           AddValidate(Check(object1,object2));
       }



        string Check(object object1, IList<object> object2)
       {

           long id = long.Parse(object1.GetType().GetProperty("ID").GetValue(object1, null).ToString());
           string value = object1.GetType().GetProperty(this.FieldName).GetValue(object1, null).ToString();
            bool active=false;
            foreach(var item in object2)
            {
                var dataID = long.Parse(item.GetType().GetProperty("ID").GetValue(item, null).ToString());
                if(id!=dataID&&!String.IsNullOrEmpty(value))
                {
                    var datavalue = (item.GetType().GetProperty(FieldName).GetValue(item, null).ToString());
                    if (object1.GetType() != typeof(Staff))
                    {
                        active = bool.Parse((item.GetType().GetProperty("Active").GetValue(item, null).ToString()));
                    }
                    if(object1.GetType()==typeof(Student))
                    {
                        if((datavalue == value ) &&!active)
                        {
                            if (ValueDefine.Lang == ValueDefine.Language.VN)
                            {
                                return NameVN + " " + ErrorCodeVN.Exist;
                            }
                            else
                            {
                                return NameEN + " " + ErrorCodeEN.Exist;
                            }
                     }
                    }
                    else
                    {

                         if(datavalue == value)
                        {
                            if (ValueDefine.Lang == ValueDefine.Language.VN)
                            {
                                return NameVN + " " + ErrorCodeVN.Exist;
                            }
                            else
                            {
                                return NameEN + " " + ErrorCodeEN.Exist;
                            }
                     }

                    }

                    
                }
            }
            return null;
              
            
       }
    }
}
